package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.*;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class SubjectiveSideDAOImpl implements SubjectiveSideDAO {

    //-----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    // ----------------------------------------------------------------

    /**
     * @return
     */
    @Override
    public List<IntentEntity> getAllIntents() {
        return sessionFactory.getCurrentSession()
                .createQuery("from IntentEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public IntentEntity getIntentByCodename(String codename) {
        return (IntentEntity) sessionFactory.getCurrentSession().
                createCriteria(IntentEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     * @return
     */
    @Override
    public List<IndiscretionEntity> getAllIndiscretionTypes() {
        return sessionFactory.getCurrentSession()
                .createQuery("from IndiscretionEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public IndiscretionEntity getIndiscretionByCodename(String codename) {
        return (IndiscretionEntity) sessionFactory.getCurrentSession().
                createCriteria(IndiscretionEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     * @return
     */
    @Override
    public List<MotiveEntity> getAllMotives() {
        return sessionFactory.getCurrentSession()
                .createQuery("from MotiveEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public MotiveEntity getMotiveByCodename(String codename) {
        return (MotiveEntity) sessionFactory.getCurrentSession().
                createCriteria(MotiveEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    @Override
    public List<PurposeEntity> getAllPurposes() {
        return sessionFactory.getCurrentSession()
                .createQuery("from PurposeEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public PurposeEntity getPurposeByCodename(String codename) {
        return (PurposeEntity) sessionFactory.getCurrentSession().
                createCriteria(PurposeEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     * @return
     */
    @Override
    public List<DelinquentStateEntity> getAllDelinquentStates() {
        return sessionFactory.getCurrentSession()
                .createQuery("from DelinquentStateEntity order by codeName").list();
    }

    /**
     * @param codename
     * @return
     */
    @Override
    public DelinquentStateEntity getDelinquentStateByCodename(String codename) {
        return (DelinquentStateEntity) sessionFactory.getCurrentSession().
                createCriteria(DelinquentStateEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }
}
